﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using ff14bot;
using Clio.Utilities;

namespace OrderUp
{
    public partial class OrderUpForm : Form
    {
        #region Variables
        public XmlDocument NewProfile = new XmlDocument();
        public Vector3 GrindAreaLocation;
        public XmlNode ProfileNode;
        public XmlNode GrindAreas;
        public XmlNode Order;
        public bool alreadysetup = false;
        #endregion

        public OrderUpForm()
        {
            InitializeComponent();
        }

        public void ClearGrindArea()
        {
            LocationLabel.Text = "<0,0,0>";
            GrindAreaLocation.X = 0;
            GrindAreaLocation.Y = 0;
            GrindAreaLocation.Z = 0;
            GrindAreaName.Text = "";
            Radius.Text = "60";
            MinLevel.Text = "1";
            MaxLevel.Text = "50";
            LessThan.Checked = true;
            GrindLevel.Text = Core.Player.ClassLevel.ToString();
            GrindMobs.Items.Clear();
        }

        public void ClearAll()
        {
            ProfileName.Text = "";
            KillRadius.Text = "80";
            ProfileNode.RemoveAll();
            NewProfile.RemoveAll();
            GrindAreas.RemoveAll();
            Order.RemoveAll();
            ClearGrindArea();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!alreadysetup)
            {
            ProfileNode = NewProfile.CreateElement("Profile");
            GrindAreas = NewProfile.CreateElement("GrindAreas");
            Order = NewProfile.CreateElement("Order");
            GrindLevel.Text = Core.Player.ClassLevel.ToString();
            timer1.Start();
            timer1.Interval = 100;
            alreadysetup = true;
            }
            /*
            if(!TreeRoot.IsRunning)
            {
                MessageBox.Show("A bot needs to be running for this plugin to work properly.  Please start a bot.");
                this.Close();
            }
            */
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void GetLocation_Click(object sender, EventArgs e)
        {
            LocationLabel.Text = Variables.playerlocation.ToString();
            GrindAreaLocation = Variables.playerlocation;
        }

        public void AddToProfile_Click(object sender, EventArgs e)
        {
            XmlElement GrindAreaNode = NewProfile.CreateElement("GrindArea");
            XmlElement Hotspots = NewProfile.CreateElement("Hotspots");
            XmlElement Hotspot = NewProfile.CreateElement("Hotspot");
            XmlElement TargetMobs = NewProfile.CreateElement("TargetMobs");
            XmlElement MinLevelNode = NewProfile.CreateElement("MinLevel");
            XmlElement MaxLevelNode = NewProfile.CreateElement("MaxLevel");
            XmlElement Grind = NewProfile.CreateElement("Grind");

            GrindAreaNode.SetAttribute("name", GrindAreaName.Text);

            Hotspot.IsEmpty = true;
            Hotspot.SetAttribute("Radius", Radius.Text);
            Hotspot.SetAttribute("X", GrindAreaLocation.X.ToString());
            Hotspot.SetAttribute("Y", GrindAreaLocation.Y.ToString());
            Hotspot.SetAttribute("Z", GrindAreaLocation.Z.ToString());
            Hotspot.SetAttribute("name", GrindAreaName.Text);
            Hotspots.AppendChild(Hotspot);
            GrindAreaNode.AppendChild(Hotspots);

            foreach(string s in GrindMobs.Items)
            {
                XmlElement TargetMob = NewProfile.CreateElement("TargetMob");
                TargetMob.IsEmpty = true;
                TargetMob.SetAttribute("name", s);
                TargetMobs.AppendChild(TargetMob);
            }
            GrindAreaNode.AppendChild(TargetMobs);

            MinLevelNode.InnerText = MinLevel.Text;
            GrindAreaNode.AppendChild(MinLevelNode);

            MaxLevelNode.InnerText = MaxLevel.Text;
            GrindAreaNode.AppendChild(MaxLevelNode);

            GrindAreas.AppendChild(GrindAreaNode);

            Grind.IsEmpty = true;
            Grind.SetAttribute("grindRef", GrindAreaName.Text);
            string operand;
            if(LessThan.Checked) { operand = "<"; } else if(EqualsTo.Checked) { operand = "="; } else { operand = ">"; }
            string whileloop = "Core.Player.ClassLevel " + operand + " " + GrindLevel.Text;
            Grind.SetAttribute("while", whileloop);
            Order.AppendChild(Grind);
            ClearGrindArea();
        }

        private void AddCurrentTarget_Click(object sender, EventArgs e)
        {
            if(Variables.hastarget)
            {
                GrindMobs.Items.Add(Variables.targetname);
            } else {
                MessageBox.Show("You need a target!");
            }
            
        }

        private void GrindMobs_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            NewProfile.Save(saveFileDialog1.FileName);
        }

        public void SaveProfile_Click(object sender, EventArgs e)
        {
            XmlNode NameNode = NewProfile.CreateElement("Name");
            NameNode.InnerText = ProfileName.Text;
            ProfileNode.AppendChild(NameNode);

            XmlNode RadiusNode = NewProfile.CreateElement("KillRadius");
            RadiusNode.InnerText = KillRadius.Text;
            ProfileNode.AppendChild(RadiusNode);

            ProfileNode.AppendChild(GrindAreas);
            ProfileNode.AppendChild(Order);

            NewProfile.AppendChild(ProfileNode);

            saveFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.ShowDialog();

            NewProfile.RemoveAll();
            ClearAll();
            this.Close();
        }

        private void OrderUpForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            NewProfile.RemoveAll();
            ClearAll();
            timer1.Stop();
            this.Close();
        }

        private void ClearGrindAreaSettings_Click(object sender, EventArgs e)
        {
            ClearGrindArea();
        }

        private void RemoveSelected_Click(object sender, EventArgs e)
        {
            GrindMobs.Items.Remove(GrindMobs.SelectedItem);
        }

        private void CloseWithoutSaving_Click(object sender, EventArgs e)
        {
            ClearAll();
            timer1.Stop();
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            using (Core.Memory.TemporaryCacheState(true))
            {
                Variables.hastarget = Core.Player.HasTarget;
                if (Core.Player.HasTarget)
                {
                    Variables.targetname = Core.Target.Name;
                }
                else
                {
                    Variables.targetname = "";
                }
                Variables.playerlocation = Core.Player.Location;
                Core.Memory.ClearCache();
            }
        }
    }
}
